#include "StdAfx.h"
#include "ApproachDrawer.h"
#include "EphemWrapper.h"

CApproachDrawer::CApproachDrawer(CApproach::Ptr pApproach, const CGridParam &gridParam)
: m_pApproach(pApproach)
{
	m_dAppPos.m_x = gridParam.getXPos(pApproach->m_pAsteroidOrbitParam->GetVariousParam());
	m_dAppPos.m_y = gridParam.getYPos(pApproach->m_dTime, -1);

	m_dHeadPos.m_x = m_dAppPos.m_x;
	m_dHeadPos.m_y = gridParam.getYPos(pApproach->m_dTime, pApproach->Dist());
	m_dAppPos.m_y = m_dHeadPos.m_y;

	m_rgbLine = GetColorByDist(pApproach->Dist());
	m_rgbPoint = GetColorByDate( pApproach->m_dTime );
}

CApproachDrawer::~CApproachDrawer(void)
{
}

CPoint CApproachDrawer::GetPos(int nWidth, int nHeight) const
{
	CPoint ptSurf(nWidth, nHeight);
	return m_dAppPos.GetPoint(ptSurf);
}

void CApproachDrawer::Draw(CDC *pCDC, int nWidth, int nHeight) const
{
	CPoint ptSurf(nWidth, nHeight);

	CPoint posApp = m_dAppPos.GetPoint(ptSurf);

	CPen penDef;
	penDef.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
	CPen * pOldDefPen = pCDC->SelectObject(&penDef);

#if 1
	CPoint posBase = m_dHeadPos.GetPoint(ptSurf);
	CPen penDist;
	penDist.CreatePen(PS_SOLID, 2, m_rgbLine);
	pCDC->SelectObject(&penDist);
	pCDC->MoveTo(posBase.x, posBase.y);
	pCDC->LineTo(posApp.x, posApp.y);

	pCDC->SelectObject(&penDef);

	CBrush brush;
	brush.CreateSolidBrush(m_rgbPoint);
	CBrush * oldBrash = pCDC->SelectObject( &brush );
	int size = 5;
	pCDC->Ellipse(posBase.x - size, posBase.y - size, posBase.x + size, posBase.y + size);
	pCDC->Ellipse(posApp.x - size, posApp.y - size, posApp.x + size, posApp.y + size);
	pCDC->SelectObject( oldBrash );
#else
	CBrush brush;
	brush.CreateSolidBrush(m_rgbPoint);
	CBrush * oldBrash = pCDC->SelectObject( &brush );
	int size = 5;
	pCDC->Ellipse(posApp.x - size, posApp.y - size, posApp.x + size, posApp.y + size);
	pCDC->SelectObject( oldBrash );
#endif
	pCDC->SelectObject(&pOldDefPen);
}

CApproach::Ptr CApproachDrawer::GetApproach() const
{
	return m_pApproach;
}


double CApproachDrawer::GetDistance(double dXpos, double dYpos) const
{
	double d1 = m_dAppPos.GetDist(dXpos, dYpos);
	double d2 = m_dHeadPos.GetDist(dXpos, dYpos);
	return min(d1, d2);
}

COLORREF CApproachDrawer::GetColorByDist(double dist)
{
	dist = CEphemWrapper::AUtoKM(dist);
	double dLoBorder = 3.0;
	double dHiBorder = 7.0;

	dist = max(dist, 1.0);
	double logDist = log10(dist);
	logDist = max(logDist, dLoBorder);
	logDist = min(logDist, dHiBorder);
	double k = (logDist - dLoBorder) / (dHiBorder - dLoBorder);
	BYTE r = BYTE(255 * (1.0 - k));
	return RGB(r, 0, 0);
}

COLORREF CApproachDrawer::GetColorByDate(double date)
{
	double Period = 365.0;
	double val = date - Period * int(date / Period);
	val = val / Period * 2.0 * M_PI;
	BYTE r = BYTE(255.0 * 0.5 * (1.0 + sin(val + 0.0 * Period/3.0)));
	BYTE g = BYTE(255.0 * 0.5 * (1.0 + sin(val + 1.0 * Period/3.0)));
	BYTE b = BYTE(255.0 * 0.5 * (1.0 + sin(val + 2.0 * Period/3.0)));
	return RGB(r, g, b);
}
